package com.mmy.service.ship.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mmy.controller.Common;
import com.mmy.dao.ship.TShipOrderRepository;
import com.mmy.dao.ship.VShipOrderRepository;
import com.mmy.domain.ship.TShipOrder;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.ship.ShipOrderService;

@Service
public class ShipOrderServiceImpl implements ShipOrderService {

	@Autowired
	TShipOrderRepository tShipOrderRepository;
	@Autowired
	VShipOrderRepository vShipOrderRepository;

	/**
	 * 个人中心通过登录人获取船票预留 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapShipReserve(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tShipOrderRepository.countByUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取船票退单 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapShipRefund(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tShipOrderRepository.countByRefundUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取从船票订单 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapShipOrder(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tShipOrderRepository.countByOrderUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取船票预留
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<TShipOrder> findByUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<TShipOrder> lstShipOrder = new ArrayList<TShipOrder>();
		lstShipOrder = tShipOrderRepository.findByUserID(iUserID, iTempPageNo, iPageSize);
		return lstShipOrder;
	}

	/**
	 * 个人中心通过登录人获取船票订单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<TShipOrder> findByOrderUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<TShipOrder> lstShipOrder = new ArrayList<TShipOrder>();
		lstShipOrder = tShipOrderRepository.findByOrderUserID(iUserID, iTempPageNo, iPageSize);
		return lstShipOrder;
	}

	/**
	 * 个人中心通过登录人获取船票退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<TShipOrder> findByChargeBackUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<TShipOrder> lstShipOrder = new ArrayList<TShipOrder>();
		lstShipOrder = tShipOrderRepository.findByChargeBackUserID(iUserID, iTempPageNo, iPageSize);
		return lstShipOrder;
	}

	/**
	 * 个人中心通过登录人取消船票预留
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TShipOrder dbShipOrder = tShipOrderRepository.findByID(iID);
		dbShipOrder.setOrderState((byte) 3);
		dbShipOrder.setModifyDate(new Date());
		dbShipOrder.setReviser(sessionSysUser.getUserName());
		tShipOrderRepository.save(dbShipOrder);
	}

	/**
	 * 个人中心通过登录人申请船票退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateRefundState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TShipOrder dbShipOrder = tShipOrderRepository.findByID(iID);
		dbShipOrder.setOrderState((byte) 4);
		dbShipOrder.setRefundProcess((byte) 1);
		dbShipOrder.setModifyDate(new Date());
		dbShipOrder.setReviser(sessionSysUser.getUserName());
		tShipOrderRepository.save(dbShipOrder);
	}

	/**
	 * 个人中心通过登录人点击撤销船票退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateCancelState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TShipOrder dbShipOrder = tShipOrderRepository.findByID(iID);
		dbShipOrder.setOrderState((byte) 2);
		dbShipOrder.setRefundProcess((byte) 0);
		dbShipOrder.setModifyDate(new Date());
		dbShipOrder.setReviser(sessionSysUser.getUserName());
		tShipOrderRepository.save(dbShipOrder);
	}

	/**
	 * 通过订单ID查询
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public TShipOrder findByID(int iID) {
		// TODO Auto-generated method stub
		return tShipOrderRepository.findByID(iID);
	}
}
